package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfAccountType;

import com.ocom.common.enums.DataSourceEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoAccountType;
import com.ocom.payhandle.service.XfAccountTypeService;
import com.ocom.redis.util.RedisUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class XfAccountTypeServiceImpl extends ServiceImpl<IDaoAccountType, EntityXfAccountType> implements XfAccountTypeService {

    @Resource
    private RedisUtil redisUtil;

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "companyId")
    @Override
    public List<EntityXfAccountType> selectAccountTypes(Long companyId) {
        QueryWrapper<EntityXfAccountType> query = Wrappers.query();
        query.eq("com_id",companyId);
        return this.list(query);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "companyId")
    public Long selectGBF(Long companyId, Integer accTypeId) {

        String key = "gbf#comId:"+companyId+"#accTypeId:"+accTypeId;
        Object gbf =  redisUtil.get(key);
        if(gbf!=null){
            return ((Integer)gbf).longValue();
        }
        QueryWrapper<EntityXfAccountType> query = Wrappers.query();
        query.select("deposit");
        query.eq("com_id",companyId);
        query.eq("acc_type_id",accTypeId);
        query.orderByDesc("update_time");
        query.last("limit 1");
        EntityXfAccountType   entityXfAccountType  = this.getOne(query);
        if(entityXfAccountType==null || entityXfAccountType.getDeposit()==null){
            return 0l;
        }
        redisUtil.set(key,entityXfAccountType.getDeposit(),3600l*4l);
        return entityXfAccountType.getDeposit();
    }


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    public EntityXfAccountType getXFAccountType(Long comId, Integer accTypeId) {
        QueryWrapper<EntityXfAccountType> query = Wrappers.query();
        query.eq("com_id",comId);
        query.eq("acc_type_id",accTypeId);
        return this.getOne(query);
    }
}
